package javolution.util;

import java.util.Collection;
import java.util.Map;
import java.util.Set;
import javolution.context.LocalContext;
import javolution.util.FastMap;

/* loaded from: classes2.dex */
public final class LocalMap<K, V> implements Map<K, V> {
    private final LocalContext.Reference _mapRef = new LocalContext.Reference(new FastMap().setShared(true));

    private FastMap<K, V> localMap() {
        FastMap<K, V> fastMap = (FastMap) this._mapRef.getLocal();
        return fastMap != null ? fastMap : newLocalMap();
    }

    private FastMap newLocalMap() {
        FastMap fastMap = (FastMap) this._mapRef.get();
        FastMap newInstance = FastMap.newInstance();
        newInstance.setShared(true);
        newInstance.setKeyComparator(fastMap.getKeyComparator());
        newInstance.setValueComparator(fastMap.getValueComparator());
        newInstance.putAll(fastMap);
        this._mapRef.set(newInstance);
        return newInstance;
    }

    @Override // java.util.Map
    public void clear() {
        FastMap<K, V> localMap = localMap();
        FastMap.Entry<K, V> head = localMap.head();
        FastMap.Entry<K, V> tail = localMap.tail();
        while (true) {
            head = head.getNext();
            if (head == tail) {
                return;
            }
            head.setValue(null);
        }
    }

    @Override // java.util.Map
    public boolean containsKey(Object obj) {
        return ((FastMap) this._mapRef.get()).containsKey(obj);
    }

    @Override // java.util.Map
    public boolean containsValue(Object obj) {
        return ((FastMap) this._mapRef.get()).containsValue(obj);
    }

    @Override // java.util.Map
    public Set<Map.Entry<K, V>> entrySet() {
        return localMap().entrySet();
    }

    @Override // java.util.Map
    public V get(Object obj) {
        return (V) ((FastMap) this._mapRef.get()).get(obj);
    }

    @Override // java.util.Map
    public boolean isEmpty() {
        return ((FastMap) this._mapRef.get()).isEmpty();
    }

    @Override // java.util.Map
    public Set<K> keySet() {
        return localMap().keySet();
    }

    @Override // java.util.Map
    public V put(K k, V v) {
        return localMap().put(k, v);
    }

    @Override // java.util.Map
    public void putAll(Map<? extends K, ? extends V> map) {
        localMap().putAll(map);
    }

    public V putDefault(K k, V v) {
        return (V) ((FastMap) this._mapRef.getDefault()).put(k, v);
    }

    /* JADX WARN: Multi-variable type inference failed */
    @Override // java.util.Map
    public V remove(Object obj) {
        return put(obj, null);
    }

    public LocalMap<K, V> setKeyComparator(FastComparator<? super K> fastComparator) {
        localMap().setKeyComparator(fastComparator);
        return this;
    }

    public LocalMap<K, V> setValueComparator(FastComparator<? super V> fastComparator) {
        localMap().setValueComparator(fastComparator);
        return this;
    }

    @Override // java.util.Map
    public int size() {
        return ((FastMap) this._mapRef.get()).size();
    }

    @Override // java.util.Map
    public Collection<V> values() {
        return localMap().values();
    }
}
